import { toFail, toSuccess, useEntityManager } from "@sker/core"
import { AutoUser, KsDevice, KsOpen } from "@sker/entities"
import { useData } from "@sker/runtime"
import { z } from "zod"

export default async () => {
    const input = z.object({
        uuid: z.string()
    })
    const body = input.parse(useData())
    return await useEntityManager(async m => {
        const device = await m.findOne(KsDevice, { where: { uuid: body.uuid } })
        if (!device) {
            return toFail(`设备不存在`)
        }
        if (!device.owner) {
            return toFail(`未绑定账号`)
        }
        const owner = await m.findOne(AutoUser, { where: { mobile: device.owner } })
        if (!owner) {
            return toFail(`账号不存在`)
        }
        // const old = await m.findOne(KsOpen, { where: { uuid: body.uuid } })
        // if (old) {
        //     return toSuccess(old)
        // }
        const item = await m.findOne(KsOpen, { where: { status: '0' } })
        if (item) return toSuccess(item)
        return toFail(`暂无数据`)
    })
}